<template>
	<view>
		<view class="uni-common-mt">
			<view class="uni-form-item uni-column">
				<view class="title">账号</view>
				<input class="uni-input" v-model="FormData.account" focus placeholder="请输入账号" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">密码</view>
				<view class="with-fun">
					<input class="uni-input" v-model="FormData.password" placeholder="请输入密码" :password="showPassword" />
					<view class="uni-icon uni-icon-eye" :class="[!showPassword ? 'uni-active' : '']" @click="changePassword"></view>
				</view>
			</view>
			<view class="uni-padding-wrap uni-common-mt">
				<button type="primary" @click="login">立即登录</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt">
				<text style="color: #007AFF;float: right;" @click="register">注册</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showPassword: true,
				FormData: {
					account: '', //1231
					password: ''
				}
			}
		},
		methods: {
			changePassword: function() {
				this.showPassword = !this.showPassword;
			},
			login() {
				let params = this.FormData
				this.$api.login(params).then((res) => {
					// 用 vuex 保存用户登录状态
					if(res.success) {
						this.$store.dispatch('addUser', res)
						uni.showToast({
							icon: 'success',
							title: "登录成功",
							duration: 500
						});
						setTimeout(() => {
							uni.switchTab({
								url:'/pages/home/home'
							})
						},500)
					}else {
						uni.showToast({
							icon: 'none',
							title: res.error,
							duration: 2000
						});
					}
				}).catch((err) => {
					console.log('request fail', err);
				})
			},
			register() {
				uni.redirectTo({
				   url: "../register/register"
				});
			}
		}
	}
</script>

<style scoped>
</style>
